package com.platform.toutiao.bean;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.platform.toutiao.error.TouTiaoErrorException;

import me.chanjar.weixin.common.util.json.GsonHelper;

public class TouTiaoAccessToken {
	private String accessToken;

	private int expiresIn = -1;

	public static TouTiaoAccessToken fromJson(String jsonStr) throws TouTiaoErrorException{
		Gson g = new Gson();
		JsonObject jsonObject = g.fromJson(jsonStr, JsonObject.class);
		if(jsonObject.get("errcode") != null) {
			throw TouTiaoErrorException.error(jsonStr);
		}
		TouTiaoAccessToken ttAccessToken = new TouTiaoAccessToken();
		ttAccessToken.setAccessToken(GsonHelper.getString(jsonObject, "access_token"));
		ttAccessToken.setExpiresIn(GsonHelper.getInteger(jsonObject, "expires_in"));
		return ttAccessToken;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public int getExpiresIn() {
		return expiresIn;
	}

	public void setExpiresIn(int expiresIn) {
		this.expiresIn = expiresIn;
	}
}
